import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { financeMachineScoreTypeArrs } from '../status.pipe';
import { CommonService } from '../../../services-common/common.service';
import { NzModalService } from 'ng-zorro-antd/modal';
import { FinanceMachineTestScoreService } from './finance-machine-test-score.service';

@Component({
  selector: 'app-finance-machine-test-score',
  templateUrl: './finance-machine-test-score.component.html',
  styleUrls: ['./finance-machine-test-score.component.scss']
})
export class FinanceMachineTestScoreComponent implements OnInit {

  searchOptions = [
    { type: 1, labelText: '综合搜索', valueKey: 'user', value: '', placeholder: 'ID/用户ID/手机号码/交易流水' },
    {
      type: 2,
      labelText: '收支类型',
      valueKey: 'type',
      value: [],
      multiple: true,
      selectBindKey: { key: 'value', label: 'label' },
      selectOptions: [...financeMachineScoreTypeArrs],
    },
    { type: 3, labelText: '发生时间', valueKey: ['beginTime', 'endTime'], value: [], isTimeSelect: true }
  ];
  searchParams: any = {};

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 20,
    total: 0
  };
  exportLoading = false;

  constructor(
    private message: NzMessageService,
    private commonService: CommonService,
    private modal: NzModalService,
    private services: FinanceMachineTestScoreService
  ) { }

  ngOnInit(): void {
  }

  async loadTable() {
    let params = {
      ...this.searchParams,
      page: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.loading = true;
    const {code, data, message}: any = await this.services.getFinanceMachineScoreList(params);
    this.tableParams.loading = false;

    if (code === 0) {
      const {records, total} = data || {};
      this.tableParams.tableData = records || [];
      this.tableParams.total = total || 0;
    } else {
      this.message.error(message);
    }
  }

  queryResetData(params: any) {
    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }

  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }

  exportTable() {
    this.modal.confirm({
      nzTitle: '提示',
      nzContent: '即将导出符合本页面筛选条件的数据，为了导出速度请尽可能的控制时间范围。确认导出？',
      nzOkText: '确认',
      nzOkType: 'primary',
      nzOkDanger: true,
      nzOnOk: async () => {
        this.exportLoading = true;
        const res: any = await this.services.exportmachineScoreData({...this.searchParams});
        this.exportLoading = false;
    
        if (res.type == 'application/octet-stream') {
          this.commonService.downLoadBlobFile(res, '验机分流水');
        } else {
          this.message.error('导出失败');
        }
      },
      nzCancelText: '取消'
    });
  }
}
